package com.lala.controller;

import com.lala.dto.ToDriverOrderDto;
import com.lala.entity.ClientOrder;
import com.lala.entity.OrderDetails;
import com.lala.service.ClientOrderService;
import com.lala.util.ResultJson;
import com.lala.entity.ClientOrder;
import com.lala.entity.OrderDetails;
import com.lala.service.ClientOrderService;
import com.lala.service.OrderDetailsService;
import com.lala.util.ResultJson;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ClientOrder)表控制层
 *
 * @author lixu
 * @since 2020-03-24 09:38:30
 */
@RestController
@RequestMapping("/clientOrder")
public class ClientOrderController {
    /**
     * 服务对象
     */
    @Resource
    private ClientOrderService clientOrderService;
    @Resource
    private OrderDetailsService orderDetailsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ClientOrder selectOne(Integer id) {
        return this.clientOrderService.queryById(id);
    }

    /**
     * @param clientId
     * @ClassName ClientOrderController
     * @Description : 根据用户id查询订单
     * @Return : com.lala.entity.ClientOrder
     * @Author : lixu
     * @Date : 2020/3/24 17:51
     */
    @GetMapping("selectByClientId")
    @ResponseBody
    public ResultJson selectAllByClientId(Integer clientId) {
        List<ClientOrder> clientOrder = clientOrderService.selectByClientId(clientId);
        return new ResultJson(200, "", clientOrder);
    }

    /**

     * @ClassName ClientOrderController
     * @Description : 查询所有订单详情
     *
     * @Return : com.lala.util.ResultJson
     * @Author : lixu
     * @Date : 2020/3/31 11:26
    */
    @GetMapping("selectAll")
    @ResponseBody
    public ResultJson selectAll(){
        List<ClientOrder> clientOrders = clientOrderService.selectAll();
        return new ResultJson(200,"",clientOrders);
    }

    /**

     * @author 霍明铭
     */
    @GetMapping("/list")
    public ResultJson list(Integer clientId, Integer status) {
        try {
            List<ClientOrder> lists = clientOrderService.getByClientIdAndStatus(clientId, status);
            System.out.println(lists);
            return new ResultJson(200, "成功", lists);
        } catch (Exception e) {
            return new ResultJson(500, "失败", null);
        }

    }


    /**
     * @param clientOrder
     * @param orderDetails
     * @ClassName ClientOrderController
     * @Description : 添加订单
     * @Return : com.lala.util.ResultJson
     * @Author : lixu
     * @Date : 2020/3/25 15:44
     */
    @PostMapping("save")
    @ResponseBody
    public ResultJson save(ClientOrder clientOrder, OrderDetails orderDetails) {
        System.out.println(orderDetails);

        try {
            int detailsId = orderDetailsService.save(orderDetails).getId();
            if (detailsId != 0) {
                int result = clientOrderService.save(clientOrder, detailsId);
                if (result != 0) {
                    System.out.println(result);
                    return new ResultJson(200, "订单创建成功", result);
                } else {
                    return new ResultJson(500, "订单创建失败", null);
                }
            } else {
                System.out.println("c");
                return new ResultJson(500, "订单详情创建失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultJson(500, "订单创建失败", null);
        }
    }

    /**
     * @param clientOrder
     * @param orderDetails
     * @ClassName ClientOrderController
     * @Description : 订单修改
     * @Return : com.lala.util.ResultJson
     * @Author : lixu
     * @Date : 2020/3/25 15:44
     */
    @PostMapping("update")
    @ResponseBody
    public ResultJson update(ClientOrder clientOrder, OrderDetails orderDetails) {
        try {
            int i = orderDetailsService.update(orderDetails);
            if (i != 0) {
                int c = clientOrderService.update(clientOrder);
                if (c != 0) {
                    return new ResultJson(200, "订单修改成功", null);
                } else {
                    return new ResultJson(500, "订单修改失败", null);
                }
            } else {
                return new ResultJson(500, "订单修改失败", null);
            }
        } catch (Exception e) {
            return new ResultJson(500, "订单修改失败", null);
        }
    }

    /**
     * @param clientOrder
     * @ClassName ClientOrderController
     * @Description : 订单删除
     * @Return : com.lala.util.ResultJson
     * @Author : lixu
     * @Date : 2020/3/25 18:07
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultJson delete(ClientOrder clientOrder) {
        try {
            boolean b = clientOrderService.deleteById(clientOrder.getId());
            if (b) {
                boolean bo = orderDetailsService.deleteById(clientOrder.getOrderDetailsid());
                if (bo) {
                    return new ResultJson(200, "订单删除成功", null);
                } else {
                    return new ResultJson(500, "订单删除失败", null);
                }
            } else {
                return new ResultJson(500, "订单删除失败", null);
            }
        } catch (Exception e) {
            return new ResultJson(500, "订单删除失败", null);
        }
    }

    @GetMapping("/money")
    @ResponseBody
    public ResultJson getMoney(Integer id) {
        double money = clientOrderService.getDetailsPay(id);
        return new ResultJson(200, "成功", money);
    }

    @GetMapping("/pay")
    @ResponseBody
    public ResultJson getPay(Integer id) {
        clientOrderService.send(id);
        return new ResultJson(200, "成功", null);
    }

    @GetMapping("/updateStatus")
    @ResponseBody
    public void updateStatus(Integer id) {
        clientOrderService.updateStatus(id);
    }

    @GetMapping("/todriver")
    @ResponseBody
    public ResultJson getJson(String orderNumber) {
        try {
            ToDriverOrderDto toDriverOrderDto = clientOrderService.getJson(orderNumber);
            return new ResultJson(200, "", toDriverOrderDto);
        } catch (Exception e) {
            return new ResultJson(500, "", null);
        }


    }

}